// File generated from our OpenAPI spec by Stainless.

package com.metest.api.services.async

import com.metest.api.core.ClientOptions
import com.metest.api.core.RequestOptions
import com.metest.api.core.handlers.errorBodyHandler
import com.metest.api.core.handlers.errorHandler
import com.metest.api.core.handlers.jsonHandler
import com.metest.api.core.http.HttpMethod
import com.metest.api.core.http.HttpRequest
import com.metest.api.core.http.HttpResponse
import com.metest.api.core.http.HttpResponse.Handler
import com.metest.api.core.http.HttpResponseFor
import com.metest.api.core.http.parseable
import com.metest.api.core.prepareAsync
import com.metest.api.models.store.StoreGetInventoryParams
import com.metest.api.models.store.StoreGetInventoryResponse
import com.metest.api.services.async.store.OrderServiceAsync
import com.metest.api.services.async.store.OrderServiceAsyncImpl
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer

class StoreServiceAsyncImpl internal constructor(private val clientOptions: ClientOptions) :
    StoreServiceAsync {

    private val withRawResponse: StoreServiceAsync.WithRawResponse by lazy {
        WithRawResponseImpl(clientOptions)
    }

    private val order: OrderServiceAsync by lazy { OrderServiceAsyncImpl(clientOptions) }

    override fun withRawResponse(): StoreServiceAsync.WithRawResponse = withRawResponse

    override fun withOptions(modifier: Consumer<ClientOptions.Builder>): StoreServiceAsync =
        StoreServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())

    override fun order(): OrderServiceAsync = order

    override fun getInventory(
        params: StoreGetInventoryParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<StoreGetInventoryResponse> =
        // get /store/inventory
        withRawResponse().getInventory(params, requestOptions).thenApply { it.parse() }

    class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
        StoreServiceAsync.WithRawResponse {

        private val errorHandler: Handler<HttpResponse> =
            errorHandler(errorBodyHandler(clientOptions.jsonMapper))

        private val order: OrderServiceAsync.WithRawResponse by lazy {
            OrderServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        override fun withOptions(
            modifier: Consumer<ClientOptions.Builder>
        ): StoreServiceAsync.WithRawResponse =
            StoreServiceAsyncImpl.WithRawResponseImpl(
                clientOptions.toBuilder().apply(modifier::accept).build()
            )

        override fun order(): OrderServiceAsync.WithRawResponse = order

        private val getInventoryHandler: Handler<StoreGetInventoryResponse> =
            jsonHandler<StoreGetInventoryResponse>(clientOptions.jsonMapper)

        override fun getInventory(
            params: StoreGetInventoryParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<StoreGetInventoryResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("store", "inventory")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { getInventoryHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }
    }
}
